//
//  SHBAVPlayerView.m
//  AVPlayerViewController_Demo
//
//  Created by 沈红榜 on 15/12/22.
//  Copyright © 2015年 沈红榜. All rights reserved.
//

#import "SHBAVPlayerView.h"

#import <AVFoundation/AVFoundation.h>

@interface SHBAVPlayerView ()<AVPlayerViewControllerDelegate>

@end

NSString * const kStatusKey         = @"status";

@implementation SHBAVPlayerView{
//    AVPlayerViewController      *_playerController;
//    AVPlayer                    *_player;
    AVAudioSession              *_session;
    NSString                    *_urlString;
//    NSString                    *_musicPathString;
//    NSString                    *_moviePathString;
}

- (id)initWithFrame:(CGRect)frame url:(NSString *)url {
    _urlString = url;
    
    self = [super initWithFrame:frame];
    if (self) {
        [self initNetWorkMediaPlayerWithPath:_urlString];
        
    }
    return self;
}
//-(id)initWithMusicPath:(NSString *)pathString;
//{
//   _musicPathString = pathString;
//    
////    self = [super initWithFrame:frame];
//    
//    if (self) {
//        [self initLocalMediaPlayerWithPath:_musicPathString];
//    }
//    return self;
//}
//
//-(id)initWithMoviePath:(NSString *)pathString
//{
//    _moviePathString = pathString;
//    
////    self = [super initWithFrame:frame];
//    if (self) {
//        [self initLocalMediaPlayerWithPath:_moviePathString];
//       
//    }
//    return self;
//}

- (id)initWithLocalMediaPath:(NSString *)pathString
{
    if (self) {
        [self initLocalMediaPlayerWithPath:pathString];
        
        
    }
    return self;
}
- (void)initLocalMediaPlayerWithPath:(NSString *)pathString
{
    [self initMediaPlayerWithPath:pathString withType:NO];
}

- (void)initNetWorkMediaPlayerWithPath:(NSString *)pathString
{
    [self initMediaPlayerWithPath:pathString withType:YES];
}

- (void)initMediaPlayerWithPath:(NSString *)pathString withType:(BOOL)isFromNetWork
{
    _session = [AVAudioSession sharedInstance];
    [_session setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    
    NSURL *url = isFromNetWork ? [NSURL URLWithString:pathString]:[NSURL fileURLWithPath:pathString];
    _player = [AVPlayer playerWithURL:url];
    _playerController = [[AVPlayerViewController alloc] init];
    _playerController.player = _player;
    _playerController.videoGravity = AVLayerVideoGravityResizeAspect;
    _playerController.delegate = self;
    _playerController.allowsPictureInPicturePlayback = true;    //画中画，iPad可用
    _playerController.showsPlaybackControls = false;
    
    _playerController.view.translatesAutoresizingMaskIntoConstraints = true;    //AVPlayerViewController 内部可能是用约束写的，这句可以禁用自动约束，消除报错
    
    [_player addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:nil];
}
- (void)dealloc
{
    [_player removeObserver:self forKeyPath:@"status"];
    
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                        change:(NSDictionary *)change context:(void *)context {
    if (object == _player && [keyPath isEqualToString:@"status"]) {
        NSLog(@"status av playser %ld",(long)_player.status);
    
        if (_player.status == AVPlayerStatusReadyToPlay) {
            self.isPauseOrNot = NO;
        } else if (_player.status == AVPlayerStatusFailed) {
            // something went wrong. player.error should contain some information
            NSLog(@"AVPlayerStatusFailed");
        }else{
            NSLog(@"ELSE");
        }
    }
}
- (void)viewWillDisappear:(BOOL)animated{
    NSLog(@"Disappear");
}
- (void)play {
    [_playerController.player play];
}
-(void)pause
{
     [_playerController.player pause];
}
#pragma mark - AVPlayerViewControllerDelegate
- (void)playerViewControllerWillStartPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@"%s", __FUNCTION__);
}

- (void)playerViewControllerDidStartPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@"%s", __FUNCTION__);
}

- (void)playerViewController:(AVPlayerViewController *)playerViewController failedToStartPictureInPictureWithError:(NSError *)error {
    NSLog(@"%s", __FUNCTION__);
}

- (void)playerViewControllerWillStopPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@"%s", __FUNCTION__);
}

- (void)playerViewControllerDidStopPictureInPicture:(AVPlayerViewController *)playerViewController {
    NSLog(@"%s", __FUNCTION__);
}

- (BOOL)playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart:(AVPlayerViewController *)playerViewController {
    NSLog(@"%s", __FUNCTION__);
    return true;
}

- (void)playerViewController:(AVPlayerViewController *)playerViewController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL))completionHandler {
    NSLog(@"%s", __FUNCTION__);
}



@end
